package sample;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2025-03-19
 * Time: 20:57
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1. 建立连接(Connection)
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置连接参数:ip,端口号,用户名,密码,虚拟机
        connectionFactory.setHost("116.205.165.92");//ip
        connectionFactory.setPort(5672);//端口号,默认是5672
        connectionFactory.setUsername("Test");//用户名
        connectionFactory.setPassword("123");//密码
        connectionFactory.setVirtualHost("TestVirtualHost");//设置操作的虚拟机
        Connection connection = connectionFactory.newConnection();//从连接工厂中获取到连接

        //2. 开启通道(Channel)
        Channel channel = connection.createChannel();//通过connection开启通道

        //3. 声明交换机(当前使用默认的即可)

        //4.声明队列
        /**
         * queueDeclare方法的参数
         * queue:队列名
         * durable:可持久化
         * exclusive:是否独占(是否只能由一个消费者进行消费)
         * autoDelete:当没有人消费时,是否自动删除
         * arguments:参数
         */
        channel.queueDeclare("testQueue", true, false, false, null);



        String msg = "HelloVampire";//消息的内容
        //发送消息
        /**
         * basicPublish方法的参数
         * exchange:交换机名称
         * routingKey:routingKey和队列名保持一致
         * props:属性配置
         * body:消息的内容
         */
        channel.basicPublish("", "testQueue", null, msg.getBytes());

        System.out.println("消息发送成功");
        //6.释放资源
        channel.close();
        connection.close();

    }
}
